<%@ page contentType="text/html;charset=UTF-8"%>
<script type="text/javascript">
	//是否服务器分页
	var _isServerPage = true;
	//资产列表
	var _assetArr=new Array();
	
	//表格列定义
	var _tableCols = 	[{
		data : "uuid",
		orderable : false,
		className : "center",
		width : "30",
		render : RapDataTableUtils.renderCheckCol
	},
	{
		data : 'contractNum',
		width : "80",
		//render : function(data, type, full){},
		orderable : true
	},
	{
		data : 'contractChangeType',
		width : "50",
		render : function(data, type, full){
			return RapDataUtils.getEnumName("stwy_contractChangeType", data);
		},
		orderable : true
	},
	{
		data : 'custom.theName',
		width : "120",
		//render : function(data, type, full){},
		orderable : true
	},
	{
		data : 'beginDate',
		width : "100",
		//render : function(data, type, full){},
		orderable : true
	},
	{
		data : 'endDate',
		width : "100",
		//render : function(data, type, full){},
		orderable : false
	},
	{
		data : 'months',
		width : "50",
		className :"rap-td-number",
		orderable : true
	},
	{
		data : 'payType',
		width : "50",
		render : function(data, type, full){
			return RapDataUtils.getEnumName("stwy_payType", data);
		},
		orderable : true
	},
	{
		data : 'price',
		width : "50",
		className :"rap-td-number",
		render:milliFormat,
		orderable : true
	},
	{
		data : 'initPay',
		width : "50",
		className :"rap-td-number",
		render:milliFormat,
		orderable : true
	},
	{
		data : 'deposit',
		width : "50",
		className :"rap-td-number",
		render:milliFormat,
		orderable : true
	},
	/* {
		data : 'hasArchives',
		width : "50",
		render : function(data, type, full){
			return RapDataUtils.getEnumName("stwy_hasArchives", data);
		},
		orderable : true
	}, */
	{
		data : "uuid",
		className : "center",
		orderable : false,
		render : function(data, type, full) {
			return "<div class='rap-btn-actiongroup'>" 
			+ "<button id='rap-btn-view-row' class='btn btn-xs btn-success' data-rel='tooltip' title='查看'><i class='fa fa-search-plus'></i></button>"
			<shiro:hasPermission name="stwy:lease:contract:update">
			+ "<button id='rap-btn-edit-row' class='btn btn-xs btn-info' data-rel='tooltip' title='编辑'><i class='fa fa-edit'></i></button>"
			</shiro:hasPermission>
			+ "</div>";
		},
		width : "80"
	}]
;
	//宏：生成ShowData方法，在编辑框显示记录数据
	function showData(data) {
		$("input[name='uuid']").val(data.uuid);
		$("input[name='theCode']").val(data.theCode);
		$("input[name='contractState']").val(data.contractState);
		$("input[name='contractChangeType']").val(data.contractChangeType);
		$("input[name='contractNum']").val(data.contractNum);
		$("input[name='customName']").val(data.custom.theName);
		$("input[name='custom.theName']").val(data.custom.theName);
		$("input[name='custom.uuid']").val(data.custom.uuid);
		$("input[name='beginDate']").val(data.beginDate);
		$("input[name='endDate']").val(data.endDate);
		
		$("input[name='signDate']").val(data.signDate);
		$("input[name='rent']").val(data.rent);
		$("select[name='increaseHandle']").val(data.increaseHandle);
		$("select[name='hasInitPay']").val(data.hasInitPay+"");
		$("input[name='area']").val(data.area);
		
		$("input[name='months']").val(data.months);
		$("select[name='payType']").val(data.payType);
		$("input[name='beforeDay']").val(data.beforeDay);
		$("input[name='price']").val(data.price);
		$("input[name='initPay']").val(data.initPay);
		$("input[name='initPayDate']").val(data.initPayDate);
		$("input[name='initPayEndDate']").val(data.initPayEndDate);
		$("select[name='increaseType']").val(data.increaseType);
		
		$("input[name='adjustDate']").val(data.adjustDate);
		$("input[name='increase']").val(data.increase);
		$("input[name='deposit']").val(data.deposit);
		$("select[name='hasArchives']").val(data.hasArchives);
		$("textarea[name='remark']").val(data.remark);
		
		$("input[name='groupOne']").val(data.groupOne);
		$("input[name='groupTwo']").val(data.groupTwo);
		
		$("input[name='govPrice']").val(data.govPrice);
		$("input[name='marketPrice']").val(data.marketPrice);
	}
	function callbackCustom(data){
		$("#customId").val(data.uuid);
		$("#customName").val(data.theName);
	}
	function calculatePrice(){
		var rent = $('#rap-form-edit input[name="rent"]').val();
		var area = $('#rap-form-edit input[name="area"]').val();
		if(rent!=null&&rent!=''&&area!=null&&area!=''&&area!=0){
			var price = rent/area;
			price = price.toFixed(4);
			$('#rap-form-edit input[name="price"]').val(price);
		}else{
			$('#rap-form-edit input[name="price"]').val('');
		}
	}
	function bindOtherAction(){
		$("#rap-form-edit select[name='hasInitPay']").bind("change",function(){
			var value = $(this).val();
			if("true"==value){
				$("#tr_initpay").removeClass("hide");
			}else{
				$("#tr_initpay").addClass("hide");
			}
		});
		$("#search_EQ_contractState").val("1");
		$("#rap-btn-quit").bind("click",function(){
			changeContract("quit");
		});
		$("#rap-btn-cancelcontract").bind("click",function(){
			changeContract("cancel");
		});
		$("#rap-btn-renew").bind("click",function(){
			onRenew();
		});
		$("#rap-btn-save-renew").bind("click",function(){
			onSaveRenew();
		});
		$("#rap-btn-cancel-renew").bind("click",function(){
			turnOutRenewMode();
		});
		$('#customName').bind('focus', function(){
			$("#rap-btn-ref-custom").click();
		});
		$('#rap-form-edit input[name="endDate"]').bind('blur input propertychange', function(){
			calculateMonths("rap-form-edit");
		});
		$('#rap-form-renew input[name="endDate"]').bind('blur input propertychange', function(){
			calculateMonths("rap-form-renew");
		});
		
		$('#rap-form-edit input[name="rent"]').bind('blur input propertychange', function(){
			calculatePrice();
		});
		
		$('#rap-form-edit input[name="area"]').bind('blur input propertychange', function(){
			calculatePrice();
		});
		
		$("#rap-btn-ref-custom").colorbox({
			iframe : true,
			opacity	: 0.2,
			transition : 'none',
			speed : 200,
			href : "${ctx}/custom/select?search_EQ_status=1&callback=callbackCustom",
			innerWidth : '90%',
			innerHeight : '80%',
			close : '×',
			fixed : true
		});
		
		$("#rap-btn-hasdeleted").colorbox({
			iframe : true,
			opacity	: 0.2,
			transition : 'none',
			speed : 200,
			href : "${serviceurl}/hasdeleted",
			innerWidth : '70%',
			innerHeight : '80%',
			close : '×',
			fixed : true
		});
	}
	
	function doAfterEdit(data){
		$("#rap-form-edit input[name='contractNum']").attr("readonly","readonly");
		showAssetList('edit');
		_contractId = data.uuid;
		loadAssetList('edit');
	}
	function doAfterView(data){
		showAssetList('view');
		_contractId = data.uuid;
		loadAssetList('view');
	}
	function doAfterBackToList(){
		hideAssetList();
	}
	function doAfterCancel(){
		hideAssetList();
	}
	function doAfterSaveSuccess(data){
		hideAssetList();
	}
	function doBeforeAdd(){
		_assetTableList.clear().draw();
		showAssetList('edit');
	}
	function doAfterAdd(){
		$("select[name='payType']").val("1");
		$("input[name='beforeDay']").val("10");
		//$("input[name='area']").attr("readonly","readonly");
		$("input[name='price']").attr("readonly","readonly");
		genContractCode();
	}
	function doBeforeSave(){
		var assetRowDatas = _assetTableList.rows().data();
		if(assetRowDatas==null||assetRowDatas.length==0){
			alert("请选择合同内资产！");
			return false;
		}
		_assetArr= new Array();
		for(var i = 0 ;i<assetRowDatas.length;i++){
			_assetArr.push(assetRowDatas[i].asset.uuid);
		}
		return true;
	}
	function genContractCode(){
		$.ajax({
			url : "${serviceurl}/genContractCode",
			dataType : 'json',
			success : function(data) {
				if(data.success){
					var code = data.records[0];
					$("#rap-form-edit input[name='contractNum']").val(code);
				}else{
					alert(data.msg);
				}
			}
		});
	}
	function calculateMonths(formId){
		var date1 = $("#"+formId+" input[name='endDate']").val();
		var date2 = $("#"+formId+" input[name='beginDate']").val();
		// 拆分年月日
		date1 = date1.split('-');
		// 得到月数
		date1 = parseInt(date1[0]) * 12 + parseInt(date1[1]);
		// 拆分年月日
		date2 = date2.split('-');
		// 得到月数
		date2 = parseInt(date2[0]) * 12 + parseInt(date2[1]);
		var m = Math.abs(date1 - date2)+1;
		$("#"+formId+" input[name='months']").val(m);
	}
	function onSave(isClose) {
		if(!doBeforeSave()){
			return false;
		}
		if (!$('#rap-form-edit').valid())
			return false;
		RapFormUtils.setCheckBoxNotCheckedValue('rap-form-edit');
		var posturl = "${serviceurl}/addContract";
		var pk = $("input[name='uuid']").val();
		if (pk != ""){
			posturl = "${serviceurl}/updateContract";
		}
		var opt = {
			url : posturl,
			type : "post",
			data : {
				"assetIds[]":_assetArr.toString()
			},
			success : function(data) {
				if(data.success){
					onRefresh();
					if (isClose) {
						_rapui.setListMode();
					} else {
						onAdd();
					}
					doAfterSaveSuccess(data);
				}
				else{
					alert("保存出现错误：" + data.msg)
				}
			}
		}
		$("#rap-form-edit").ajaxSubmit(opt);
	}
	//续签
	function onRenew(){
		var selectRowDatas = RapDataTableUtils.getSelectRowDatas(_tableList);
		if(selectRowDatas==null||selectRowDatas.length==0){
			alert("请选择要续签的合同");
			return;
		}
		if(selectRowDatas.length!=1){
			alert("只能选择一份合同进行续签");
			return;
		}
		var data = selectRowDatas[0];
		showData(data);
		turnToRenewMode(data);
	}
	function turnToRenewMode(data){
		$("#rap-page-area-renew").removeClass("hide");
		showAssetList('view');
		_contractId = data.uuid;
		loadAssetList('view');
		
		$("#rap-page-area-list").addClass("hide"); 
	}
	function turnOutRenewMode(){
		$("#rap-page-area-renew").addClass("hide");
		hideAssetList();
		$("#rap-page-area-list").removeClass("hide");
	}
	function onSaveRenew() {
		if (!$('#rap-form-renew').valid())
			return false;
		RapFormUtils.setCheckBoxNotCheckedValue('rap-form-renew');
		var posturl = "${serviceurl}/renew";
		var opt = {
			url : posturl,
			type : "post",
			success : function(data) {
				if(data.success){
					onRefresh();
					turnOutRenewMode();
				}
				else{
					alert("保存出现错误：" + data.msg)
				}
			}
		}
		$("#rap-form-renew").ajaxSubmit(opt);
	}
	$(document).ready(function() {
		bindDefaultActions();
		bindOtherAction();
		_queryData = $("#rap-form-query").serializeArray();
		if(_isServerPage){
			//服务器分页
			serverPage();	
		}else{
			//非服务器分页
			_tableList = $('#rap-table-list').DataTable({
				"columns" : _tableCols,
				"createdRow" : RapDataTableUtils.setActions,
				"order": [] 
			});
			loadList();		
		}
		
		RapDataTableUtils.setCheckCol(_tableList);

		/**
		 * 表单校验
		 */
		_validator = $('#rap-form-edit').validate({
					rules : {
						contractNum : {
							required : true
						},
						customName : {
							required : true
						},
						beginDate : {
							required : true
						},
						endDate : {
							required : true
						},
						rent : {
							required : true,
							isNumber:true
						},
						price : {
							required : true,
							isNumber:true
						},
						payType : {
							required : true
						},
						beforeDay : {
							isNumber:true
						},
						initPay :{
							required : true,
							isNumber:true
						},
						initPayDate :{
							required : true
						},
						initPayEndDate :{
							required : true
						},
						deposit : {
							required : true,
							isNumber:true
						}
					},
					messages : {
						rent : {
							isNumber:"请输入数字"
						},
						price : {
							isNumber:"请输入数字"
						},
						increase : {
							isNumber:"请输入数字"
						},
						deposit : {
							isNumber:"请输入数字"
						}
					}
				});
		$('#rap-form-renew').validate({
			rules : {
				endDate : {
					required : true
				},
				rent : {
					required : true,
					isNumber:true
				},
				price : {
					required : true,
					isNumber:true
				},
				payType : {
					required : true
				},
				beforeDay : {
					isNumber:true
				}
			},
			messages : {
				
			}
		});
	});
</script>